<?php

namespace app\mp\controller;

use app\common\baidu\Trans;
use app\common\entity\BackResult;
use app\common\entity\Result;
use app\Request;
use think\facade\Db;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/6 0006
 * Time: 16:59
 */
class Word extends Base
{
    /**
     * 翻译词汇
     * @param Request $request
     * @return \think\Response
     */
    public function trans(Request $request)
    {
        $word = trim($request->param('word'));
        if (empty($word)) {
            return BackResult::error(null, '没有目标词汇');
        }
        return Result::success($this->getTrans($word));
    }

    /**
     * 添加到我的生词本
     * @param Request $request
     * @return \think\Response
     */
    public function add(Request $request)
    {
        $word = trim($request->param('word'));
        if (empty($word)) {
            return BackResult::error(null, '没有目标词汇');
        }
        if (!Db::table('user_favor_vocabulary')->where(['uid' => $this->user['id'], 'word' => $word])->value('id')) {
            Db::table('user_favor_vocabulary')->insert([
                'uid' => $this->user['id'],
                'word' => $word,
                'trans' => json_encode($this->getTrans($word)),
                'create_time' => date('Y-m-d H:i:s')
            ]);
        }
        return BackResult::success();
    }

    /**
     * 获取生词本列表
     * @param Request $request
     * @return \think\Response
     */
    public function getList(Request $request)
    {
        $page = (int)$request->param('page', 1);
        $rows = Db::table('user_favor_vocabulary')
            ->where('uid', $this->user['id'])
            ->order('id', 'desc')
            ->page($page, 10)
            ->column('id,word,trans');
        foreach ($rows as &$row) {
            $row['trans'] = json_decode($row['trans']);
        }
        return Result::success($rows);
    }

    /**
     * @param Request $request
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function del(Request $request)
    {
        $id = (int)$request->param('id');
        if (!$id || !Db::table('user_favor_vocabulary')->where(['id' => $id, 'uid' => $this->user['id']])->find()) {
            return BackResult::error(null, '不存在的单词');
        }
        Db::table('user_favor_vocabulary')->where('id', $id)->delete();
        return BackResult::success();
    }

    /**
     * @param $word
     * @return array
     */
    private function getTrans($word)
    {
        $result = Trans::translate($word, 'en', 'zh')['trans_result'][0];
        $data = [
            'dst' => $result['dst'],
            'tts' => $result['src_tts'],
            'pronunciation' => [],
            'parts' => []
        ];
        $dict = $result['dict'] ? json_decode($result['dict'], true) : null;
        if ($dict) {
            foreach ($dict['word_result']['simple_means']['symbols'] as $symbols) {
                $data['pronunciation'][] = $symbols['ph_en'];
                $data['parts'][] = $symbols['parts'];
            }
        }
        return $data;
    }
}